package trunk.frontend;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.border.EtchedBorder;

import trunk.backend.Page;

public class WFBFrame extends JFrame implements WindowListener, ActionListener {

	/**
	 * Default value
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Yes
	 */
	private static final int YES_OPTION = 0;
	
	/**
	 * No
	 */
	private static final int NO_OPTION = 1;	
	
	/**
	 * Cancel
	 */
	private static final int CANCEL_OPTION = 2;
	
	/**
	 * Label
	 */
	private static final int LABEL_OPTION = 0;
	
	/**
	 * Text Box
	 */
	private static final int TEXTBOX_OPTION = 1;
	
	/**
	 * Check Box
	 */
	private static final int CHECKBOX_OPTION = 2;
	
	/**
	 * Combo Box
	 */
	private static final int COMBOBOX_OPTION = 3;
	
	/**
	 * Radio Button
	 */
	private static final int RADIO_OPTION = 4;
	
	/**
	 * Likert Scale
	 */
	private static final int LIKERT_OPTION = 5;
	
	/**
	 * Delimeter
	 */
	private static final String DELIMITER_OPTION = "#:@";
	
	/**
	 * Array Seperator (within strings)
	 */
	private static final String ARRAYSEPERATOR_OPTION = "#:%";
	
	/**
	 * Increment Document number
	 */
	private int documentNumber = 0;
	
	/**
	 * The main menu bar
	 */
    JMenuBar menuBar = new JMenuBar();
    
    /**
     * The file menu
     */
    JMenu file = new JMenu("File");
    
    /**
     * The new menu item
     */
    JMenuItem newMI;
    
    /**
     * The open menu item
     */
    JMenuItem openMI;
    
    /**
     * The save menu item
     */
    JMenuItem saveMI;
    
    /**
     * The save as menu item
     */
    JMenuItem saveAsMI;
    
    /**
     * The exit menu item
     */
    JMenuItem exitMI;
    
    /**
     * The form builder menu
     */
    JMenu form = new JMenu("Web Form");
    
    /**
     * Generates the web form
     */
    JMenuItem generate;
    
    /**documentNumber
     * The help menu
     */
    JMenu help = new JMenu("Help");

    /**
     * Opens the user manual
     */
    JMenuItem manual;
    
    /**
     * Opens the about
     */
    JMenuItem about;
    
    /**
     * The main panel
     */
    JPanel panel1 = new JPanel();
    
    /**
     * Panel used for positioning of form elements
     */
    private JPanel formPanel;
    
    /**
     * Panel that actually contains all form elements
     * (technically, holds the JPanel that encapsulates
     * a single component)
     */
    private JPanel innerFormPanel;
    
    /**
     * The scroll pane for the form display
     */
    JScrollPane scrollPane = new JScrollPane(panel1);
	
    /**
     * The file chooser for opening and saving files
     */
    JFileChooser jfc = new JFileChooser();
    
    /**
     * The file the user is working on
     */
    File webForm;
    
    /**
     * Button to add a check box to the form
     */
    private JButton jbAddCheckBox;
    
    /**
     * Button to add a combo box to the form
     */
    private JButton jbAddComboBox;
    
    /**
     * Button to add a single-line text field to the form
     */
    private JButton jbAddLikertScale;
    
    /**
     * Button to add a multi-line text area to the form
     */
    private JButton jbAddTextArea;
    
    /**
     * Button to add a radio button to the form
     */
    private JButton jbAddRadioButton;
    
    /**
     * Button to add a label to the form
     */
    private JButton jbAddLabel;
    
    /**
     * Button that removes the last component added by a user
     */
    private JButton jbUndoLast;
    
    /**
     * Layout manager for all sub panels
     */
    private GridBagLayout gbl = new GridBagLayout();
    
    /**
     * Constraints for the layout manager
     */
	private GridBagConstraints gbc = new GridBagConstraints();
	
	/**
	 * Array to track all added components
	 */
	private ArrayList <String> formComponents = new ArrayList <String>();
	
	/**
	 * Array to track the java web form components
	 */
	private ArrayList <JComponent> javaFormComponents = new ArrayList <JComponent>();
    
	/**
	 * Initializes a new WFBFrame
	 */
	public WFBFrame () {
		super("Web Form Builder");
		initializeMenu();
		initializeContentPane();
		initializeFrame();
		initialize(false);
		//testClass();
	}
	
	private void testClass() {
		String labels = "you" + DELIMITER_OPTION + "me" + DELIMITER_OPTION;
		String values = ARRAYSEPERATOR_OPTION + DELIMITER_OPTION 
			+ "0" + DELIMITER_OPTION + "1";
		addToForm(LABEL_OPTION + DELIMITER_OPTION + "Test Label");
		addToForm(TEXTBOX_OPTION + DELIMITER_OPTION + "Test Box" 
				+ DELIMITER_OPTION  + "This is a test");
		addToForm(CHECKBOX_OPTION + DELIMITER_OPTION + "Check Box" 
				+ DELIMITER_OPTION  + labels + values);
		addToForm(RADIO_OPTION + DELIMITER_OPTION + "Radio Buttons" 
				+ DELIMITER_OPTION  + labels + values);
		addToForm(LIKERT_OPTION + DELIMITER_OPTION + "Likert Scale" 
				+ DELIMITER_OPTION  + labels + values);
	}



	/**
	 * Initializes the content of the program (such as when "new" is called)
	 */
	private void initialize(boolean loadFile) {
		formComponents = new ArrayList <String>();
		javaFormComponents = new ArrayList <JComponent>();
		clearJavaForm();
		if (loadFile) {
			String line;
			try {
				BufferedReader br = new BufferedReader(new FileReader(webForm));
				while ((line = br.readLine()) != null) {
					//formComponents.add(line);
					addToForm(line);
					//System.out.println(line);
				}
		        br.close();
			} catch(IOException e) {
				System.out.println("Load Failed! Blame Java!");
			}
		} else {
			documentNumber++;
			webForm = new File("Webform " + documentNumber + ".frm");
		}
		//formComponents.add(LABEL_OPTION + DELIMITER_OPTION + "Test Label");
		//repaintJavaForm();
	}
	
	/**
	 * Initializes the menu
	 */
	public void initializeMenu() {
		menuBar.add(file);
		menuBar.add(form);
		menuBar.add(help);
		
		newMI = new JMenuItem("New");
		newMI.setAccelerator(KeyStroke.getKeyStroke(
		                KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		newMI.addActionListener(this);
		file.add(newMI);
		
		openMI = new JMenuItem("Open");
		openMI.setAccelerator(KeyStroke.getKeyStroke(
		                KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		openMI.addActionListener(this);
		file.add(openMI);
		
		file.addSeparator();
		
		saveMI = new JMenuItem("Save");
		saveMI.setAccelerator(KeyStroke.getKeyStroke(
		                KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		saveMI.addActionListener(this);
		file.add(saveMI);
		
		saveAsMI = new JMenuItem("Save As");
		saveAsMI.addActionListener(this);
		file.add(saveAsMI);
		
		file.addSeparator();
		
		exitMI = new JMenuItem("Exit");
		exitMI.setAccelerator(KeyStroke.getKeyStroke(
		                KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
		exitMI.addActionListener(this);
		file.add(exitMI);

		generate = new JMenuItem("Generate Web Form");
		generate.setAccelerator(KeyStroke.getKeyStroke(
		                KeyEvent.VK_G, ActionEvent.CTRL_MASK));
		generate.addActionListener(this);
		form.add(generate);
		
		manual = new JMenuItem("User Manual");
		manual.addActionListener(this);
		help.add(manual);
		
		about = new JMenuItem("About Web Form Generator");
		about.addActionListener(this);
		help.add(about);
	}
	
	/**
	 * Initializes the main content areas
	 */
	public void initializeContentPane() {
		JPanel buttonPanel = new JPanel();
		JScrollPane jspFormScroller;
		//JLabel jlFakeLabel = new JLabel("Fake text here:");
		
		formPanel = new JPanel();
		//formPanel.setPreferredSize(new Dimension(450, 450));
		jspFormScroller = new JScrollPane(formPanel);
		jspFormScroller.setPreferredSize(new Dimension(450, 450));
        innerFormPanel = new JPanel();
		
		initializeButtons();
		
		buttonPanel.setLayout(gbl);
		formPanel.setLayout(gbl);
		innerFormPanel.setLayout(gbl);
		panel1.setLayout(gbl);
		
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridx = 0;
		gbc.weightx = 1.0;			JTextField jtfFakeText = new JTextField(15);
		addToForm(jtfFakeText);
		gbc.weighty = 1.0;
		formPanel.add(innerFormPanel, gbc);
		
		gbc.anchor = GridBagConstraints.NORTH;
		gbc.gridx = 0;
		gbc.gridy = GridBagConstraints.RELATIVE;
		gbc.insets = new Insets(10, 10, 10, 10);
	     
		
		buttonPanel.add(jbAddLabel, gbc);
		buttonPanel.add(jbAddTextArea, gbc);
		buttonPanel.add(jbAddCheckBox, gbc);
		//buttonPanel.add(jbAddComboBox, gbc);
		buttonPanel.add(jbAddRadioButton, gbc);
		buttonPanel.add(jbAddLikertScale, gbc);
		//buttonPanel.add(jbUndoLast, gbc);
		
		gbc.insets = new Insets(0, 0, 0, 0);
		
		//addToForm(jlFakeLabel);
		formPanel.setBackground(Color.white);
		formPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridx = GridBagConstraints.RELATIVE;
		gbc.gridy = 0;
		
		panel1.add(buttonPanel, gbc);
		panel1.add(jspFormScroller, gbc);
	}
	
	/**
	 * Initializes the buttons
	 */
	private void initializeButtons() {
		Dimension dimButton = new Dimension(175, 25);
		
		jbAddCheckBox = new JButton("Add Check Boxes");
		jbAddCheckBox.setSize(dimButton);
		jbAddCheckBox.setMinimumSize(dimButton);
		jbAddCheckBox.setMaximumSize(dimButton);
		jbAddCheckBox.setPreferredSize(dimButton);
		jbAddCheckBox.addActionListener(this);
		
		jbAddComboBox = new JButton("Add Combo Boxes");
		jbAddComboBox.setSize(dimButton);
		jbAddComboBox.setMinimumSize(dimButton);
		jbAddComboBox.setMaximumSize(dimButton);
		jbAddComboBox.setPreferredSize(dimButton);
		jbAddComboBox.addActionListener(this);
		
		jbAddLikertScale = new JButton("Add Likert Scale");
		jbAddLikertScale.setSize(dimButton);
		jbAddLikertScale.setMinimumSize(dimButton);
		jbAddLikertScale.setMaximumSize(dimButton);
		jbAddLikertScale.setPreferredSize(dimButton);
		jbAddLikertScale.addActionListener(this);
		
		jbAddTextArea = new JButton("Add Text Box");
		jbAddTextArea.setSize(dimButton);
		jbAddTextArea.setMinimumSize(dimButton);
		jbAddTextArea.setMaximumSize(dimButton);
		jbAddTextArea.setPreferredSize(dimButton);
		jbAddTextArea.addActionListener(this);
		
		jbAddRadioButton = new JButton("Add Radio Buttons");
		jbAddRadioButton.setSize(dimButton);
		jbAddRadioButton.setMinimumSize(dimButton);
		jbAddRadioButton.setMaximumSize(dimButton);
		jbAddRadioButton.setPreferredSize(dimButton);
		jbAddRadioButton.addActionListener(this);
		
		jbAddLabel = new JButton("Add Label");
		jbAddLabel.setSize(dimButton);
		jbAddLabel.setMinimumSize(dimButton);
		jbAddLabel.setMaximumSize(dimButton);
		jbAddLabel.setPreferredSize(dimButton);
		jbAddLabel.addActionListener(this);
		
		jbUndoLast = new JButton("Undo Last");
		jbUndoLast.setSize(dimButton);
		jbUndoLast.setMinimumSize(dimButton);
		jbUndoLast.setMaximumSize(dimButton);
		jbUndoLast.setPreferredSize(dimButton);
		jbUndoLast.addActionListener(this);
	}
	
	/**
	 * Initializes the frames
	 */
	public void initializeFrame() {
		addWindowListener(this);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setSize(new Dimension(700, 550));
		setJMenuBar(menuBar);
		getContentPane().add(scrollPane);
	}
	
	/**
	 * Adds a given component to the java web form
	 * @param comp the properly delimited compontent to add
	 */
	public void addToForm(String comp) {
		//System.out.println("AddToForm string called");
		//String name;
		String temp;
		int counter;
		ArrayList <String> labels = new ArrayList <String>();
		JVerticalPanel jvp;
		JPanel jp;
		JRadioButton jrb;
		ButtonGroup bg;
		String[] strTok;
		formComponents.add(comp);
		strTok = comp.split(DELIMITER_OPTION);
		System.out.println(strTok[0]);
		switch(Integer.parseInt(strTok[0])) {
		case LABEL_OPTION:
			JLabel label = new JLabel(strTok[1]);
			javaFormComponents.add(label);
			addToForm(label);
			break;
		case TEXTBOX_OPTION:
			//strTok.nextToken();
			jp = new JPanel();
			jp.add(new JLabel(strTok[1]));
			if (strTok.length < 3) {
				jp.add(new JTextArea(1, 15));
			} else {
				jp.add(new JTextArea(strTok[2], 1, 15));
			}
			javaFormComponents.add(jp);
			addToForm(jp);
			break;
		case CHECKBOX_OPTION:
			//name = strTok.nextToken();
			jvp = new JVerticalPanel();
			//jvp.add(new JLabel(strTok[1]));
			counter = 2;
			System.out.println(strTok.length);
			while (counter < strTok.length ) {
				System.out.println("Looping");
				if(!(temp = strTok[counter]).contains(ARRAYSEPERATOR_OPTION)){
					labels.add(temp);
					counter++;
				}
				else {
					break;
				}
			}
			
						
			for (int i = 0; i < labels.size(); i++) {
				jvp.add(new JCheckBox(labels.get(i)));
			}
			javaFormComponents.add(jvp);
			addToForm(jvp);
			//page.addCheckBoxes(name, labels, values, true);
			break;
		case COMBOBOX_OPTION:
			System.out.println("Combo Box Not Implemented, Sorry.");
			break;
		case RADIO_OPTION:
			//name = strTok.nextToken();
			jvp = new JVerticalPanel();
			//jvp.add(new JLabel(strTok[1]));
			bg = new ButtonGroup();
			counter = 2;
			while (!(temp = strTok[counter]).equals(ARRAYSEPERATOR_OPTION)) {
				labels.add(temp);
				counter++;
			}
			for (int i = 0; i < labels.size(); i++) {
				jrb = new JRadioButton(labels.get(i));
				bg.add(jrb);
				jvp.add(jrb);
			}
			javaFormComponents.add(jvp);
			addToForm(jvp);
			//page.addRadioButton(name, labels, values, true);
			break;
		case LIKERT_OPTION:
			//name = strTok.nextToken();
			jp = new JPanel();
			//jp.add(new JLabel(strTok[1]));
			bg = new ButtonGroup();
			counter = 2;
			while (!(temp = strTok[counter]).equals(ARRAYSEPERATOR_OPTION)) {
				labels.add(temp);
				counter++;
			}
			for (int i = 0; i < labels.size(); i++) {
				jrb = new JRadioButton(labels.get(i));
				bg.add(jrb);
				jp.add(jrb);
			}
			javaFormComponents.add(jp);
			addToForm(jp);
			//page.addRadioButton(name, labels, values, false);
			break;
		default:
			System.out.println("Invalid Form Type. Contact Code Writers :(");
		}
		innerFormPanel.setVisible(false);
		innerFormPanel.setVisible(true);
	}
	
	private void addToForm(JComponent jcomp) {
		JPanel jptemp = new JPanel();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridx = 0;
		gbc.gridy = GridBagConstraints.RELATIVE;
		jptemp.setAlignmentX(0);
		jptemp.setAlignmentY(0);
		jptemp.setLayout(gbl);
		jptemp.add(jcomp, gbc);
		innerFormPanel.add(jptemp, gbc);
	}
	
	/**
	 * Removes a specific component
	 */
	private void removeComponent(JComponent jc) {
		innerFormPanel.remove(jc);
	}
	
	/**
	 * Clears out the java form
	 */
	private void clearJavaForm() {
		innerFormPanel.removeAll();
		innerFormPanel.setVisible(false);
		innerFormPanel.setVisible(true);
	}
	
	/*private void repaintJavaForm() {
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridx = 0;
		gbc.gridy = GridBagConstraints.RELATIVE;
		
		formPanel.removeAll();
		
		for(int i = 0; i < alComponents.size(); i++) {
			formPanel.add((JPanel)alComponents.get(i), gbc);
		}
	}*/
	
	/**
	 * Handles the "Do you want to Save" option
	 * @return yes, no, cancel
	 */
	public int saveConfirmation() {
		String name;
		if (null == webForm) {
			name = "WebForm1";
		} else {
			name = webForm.getName();
		}
		switch(JOptionPane.showConfirmDialog(null, 
				"Do you want to save changes to " + name + "?", 
				"Web Form Builder", JOptionPane.YES_NO_CANCEL_OPTION )) {
			case JOptionPane.YES_OPTION:
				return YES_OPTION;
			case JOptionPane.NO_OPTION:
				return NO_OPTION;
			case JOptionPane.CANCEL_OPTION:
				return CANCEL_OPTION;
			default:
				return -1;
		}
	}
	
	/**
	 * Handles creating a new document
	 */
	public void newDocument() {
		switch(saveConfirmation()) {
		case YES_OPTION:
			saveDocument();
			webForm = null;
			initialize(false);
			break;
		case NO_OPTION:
			webForm = null;setVisible(false);
			initialize(false);
			break;
		case CANCEL_OPTION:
			break;
		default:
			System.out.println("Invalid Argument in Close Program");
			System.exit(1);
		}	
	}

	/**
	 * Handles opening a new document
	 */
	public void openDocument() {
		switch(saveConfirmation()) {
		case YES_OPTION:
			saveDocument();
			if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
				webForm = jfc.getSelectedFile();
		        initialize(true);
			}
			break;
		case NO_OPTION:
			if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
				webForm = jfc.getSelectedFile();
				initialize(true);
			}
			break;
		case CANCEL_OPTION:
			break;
		default:
			System.out.println("Invalid Argument in Close Program");
			System.exit(1);
		}	
	}
	
	/**
	 * Handles saving a new document
	 */
	public void saveDocument() {
		if (null == webForm) {
			saveDocumentAs();
			return;
		}
		try {
			PrintWriter pw = new PrintWriter(new FileWriter(webForm));
	        for (int i = 0; i < formComponents.size(); i++) {
	        	pw.println(formComponents.get(i));
	        }
	        pw.close();
		} catch(IOException e) {
			System.out.println("Save Failed! Blame Java!");
		}
	}
	
	/**
	 * Handles saving a document as
	 */
	public void saveDocumentAs() {
		if (jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
			webForm = jfc.getSelectedFile();
			if (!webForm.getName().contains(".frm")) {
				webForm = new File(webForm.getAbsolutePath() + ".frm");
			}
	        saveDocument();
	    }
	}
	
	/**
	 * Handles window closing
	 */
	public void closeProgram() {
		switch(saveConfirmation()) {
			case YES_OPTION:
				saveDocument();
				System.exit(0);
				break;
			case NO_OPTION:
				System.exit(0);
				break;
			case CANCEL_OPTION:
				break;
			default:
				System.out.println("Invalid Argument in Close Program");
				System.exit(1);
		}	
	}
	
	/**
	 * Shows the "really want to quit" dialogue before exiting
	 * @param e the event
	 */
    public void windowClosing(WindowEvent e) {
    	closeProgram();
    }
    
    /**
     * Generates the web form and saves it as an html document
     */
	private void generateWebForm() {
		Page page = new Page();
		int counter;
		String name;
		String temp;
		ArrayList <String> labels;
		ArrayList <String> values;
		String[] strTok; 
		for (int i = 0; i < formComponents.size(); i++) {
			strTok = formComponents.get(i).split(DELIMITER_OPTION);
			switch(Integer.parseInt(strTok[0])) {
			case LABEL_OPTION:
				page.addLabel(strTok[1], true);
				break;
			case TEXTBOX_OPTION:
				if (strTok.length > 2) {
					page.addTextBox(strTok[1], strTok[2]);
				}
				else {
					page.addTextBox(strTok[1], "");
				}
				break;
			case CHECKBOX_OPTION:
				labels = new ArrayList <String>();
				values = new ArrayList <String>();
				name = strTok[1];
				counter = 2;
				while (!(temp = strTok[counter]).equals(ARRAYSEPERATOR_OPTION)) {
					labels.add(temp);
					counter++;
				}
				counter++;
				for (int j = counter; j < strTok.length; j++) {
					values.add(strTok[j]);
				}
				page.addCheckBoxes(name, labels, values, true);
				break;
			case COMBOBOX_OPTION:
				System.out.println("Combo Box Not Implemented, Sorry.");
				break;
			case RADIO_OPTION:
				labels = new ArrayList <String>();
				values = new ArrayList <String>();
				name = strTok[1];
				counter = 2;
				while (!(temp = strTok[counter]).equals(ARRAYSEPERATOR_OPTION)) {
					labels.add(temp);
					counter++;
				}
				counter++;
				for (int j = counter; j < strTok.length; j++) {
					values.add(strTok[j]);
				}
				page.addRadioButton(name, labels, values, true);
				break;
			case LIKERT_OPTION:
				labels = new ArrayList <String>();
				values = new ArrayList <String>();
				name = strTok[1];
				counter = 2;
				while (!(temp = strTok[counter]).equals(ARRAYSEPERATOR_OPTION)) {
					labels.add(temp);
					counter++;
				}
				counter++;
				for (int j = counter; j < strTok.length; j++) {
					values.add(strTok[j]);
				}
				page.addRadioButton(name, labels, values, false);
				break;
			default:
				System.out.println("Invalid Form Type. Contact Code Writers :(");
			}
		}
		page.writeHTML(webForm.getAbsolutePath().replaceFirst(".frm", ".html"), "");
	}

    /**
     * Button Listener
     * @author David Hendershot (gtg396y@mail.gatech.edu)
     *
     */
    /*private class ButtonListener implements ActionListener {
        public void actionPerformed (ActionEvent event) {
            if (event.getSource() == userSearchButton) {
                    searchButtonPushed = 1;
            } else if (event.getSource() == fieldSearchButton) {
                    searchButtonPushed = 2;
            } else if (event.getSource() == authorSearchButton) {
                    searchButtonPushed = 3;
            } else if (event.getSource() == exitButton) {
                exitButtonPushed = true;
            } else if (event.getSource() == newQuoteButton) {
                String hold = authorNQTF.getText() + ": " + titleNQTF.getText();
                myALQuotes.add(hold);
            	quotesUpdated = true;
            }
        }
    }*/
    
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * Handles all events presses
	 * @param e the event
	 */
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == newMI) {
			newDocument();
		} else if(e.getSource() == openMI) {
			openDocument();
		} else if(e.getSource() == saveMI) {
			saveDocument();
		} else if(e.getSource() == saveAsMI) {
			saveDocumentAs();
		} else if(e.getSource() == exitMI) {
			closeProgram();
		} else if(e.getSource() == generate) {
			generateWebForm();
		} else if(e.getSource() == manual) {
			System.out.println("Implement User Manual");
		} else if(e.getSource() == about) {
			System.out.println("Implement About");
		} else if(e.getSource() == jbAddLabel) {
			LabelOptions lo = new LabelOptions(this);
		} else if(e.getSource() == jbAddTextArea) {
			TextBoxOptions tbo = new TextBoxOptions(this);
		} else if(e.getSource() == jbAddCheckBox) {
			new CheckboxOptions(this);
		} else if(e.getSource() == jbAddRadioButton) {
			new RadioButtonOptions(this);
		} else if(e.getSource() == jbAddLikertScale) {
			new LikertScaleOptions(this);
		} else if(e.getSource() == jbUndoLast) {
			Component[] comps = innerFormPanel.getComponents();
			for(int i = 0; i < comps.length; i++) {
				System.out.println(i + ": " + comps[i]);
				System.out.println(i + ": " + javaFormComponents.get(i));
			}
			innerFormPanel.remove(javaFormComponents.get(javaFormComponents.size() - 1));
		}
	}
}